'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var TreeView = function () {
    function TreeView() {
        _classCallCheck(this, TreeView);

        var that = this;

        this.nodes = [];
        this.selectedNode = undefined;
        this.collapsed = [];
    }

    _createClass(TreeView, [{
        key: 'iterate',
        value: function iterate(list, node) {
            for (var i = 0; i < list.length; i++) {
                var found = false;

                if (list[i] == node) {
                    found = true;
                } else if (angular.isDefined(list[i].children)) {
                    found = this.iterate(list[i].children);
                }

                if (found == true) {
                    this.collapsed.push(list[i]);
                    return true;
                }
            }
            return false;
        }
    }, {
        key: 'collapseTo',
        value: function collapseTo(node) {
            this.iterate(this.nodes, node);
        }
    }, {
        key: 'collapseSub',
        value: function collapseSub(node) {
            this.collapsed.push(node);
            if (angular.isDefined(node.children)) {
                var that = this;
                node.children.forEach(function (nd) {
                    return that.collapseSub(nd);
                });
            }
        }
    }, {
        key: 'collapseAll',
        value: function collapseAll() {
            var _this = this;

            this.collapsed = [];
            this.nodes.forEach(function (node) {
                return _this.collapseSub(node);
            });
        }
    }]);

    return TreeView;
}();

angular.module('ui.bootstrap.treeview', []).directive('treeView', ['$compile', '$templateCache', function ($compile, $templateCache) {
    return {
        restrict: 'E',
        scope: {},
        bindToController: {
            treeService: '='
        },
        controller: function controller() {
            var that = this;

            this.isCollapsed = function (node) {
                return that.treeService.collapsed.indexOf(node) > -1;
            };

            // toggle when icon clicked
            this.toggleNode = function (node) {
                if (!node.children) return;

                // collapse / expand
                if (node.children && node.children.length > 0) {
                    // add the node to our collapsed array
                    var index = that.treeService.collapsed.indexOf(node);

                    if (index == -1) that.treeService.collapsed.push(node);else that.treeService.collapsed.splice(index, 1);
                }
            };

            // select when name clicked
            this.selectNode = function (e, node) {
                that.treeService.selectedNode = node;

                e.stopPropagation();
                e.preventDefault();
            };
        },
        controllerAs: 'ctrl',
        link: function link(scope, element, attrs, ctrl) {
            var isRoot = !attrs.treeRoot ? true : false;
            var nodeLabel = attrs.nodeLabel || 'label';
            var itemClass = attrs.itemClass || '';
            var itemInclude = attrs.itemNgInclude || '';
            var itemIncludeHtml = '';

            if (itemInclude && itemInclude.length > 0) {
                itemIncludeHtml = $templateCache.get(attrs.itemNgInclude);
            }

            // remove attributes
            element.removeAttr('node-label');
            element.removeAttr('item-class');
            element.removeAttr('item-ng-include');
            element.removeAttr('tree-root');

            // template
            var template = '<ul>' + '<li ng-repeat="node in [REPLACENODES]">' + '<div ng-click="ctrl.selectNode($event, node)" class="node" ng-class="{\'selected\' : node == ctrl.treeService.selectedNode}">' + '<div ' + (itemClass != '' ? ' class="' + itemClass + '"' : '') + '>' + '<i ng-click="ctrl.toggleNode(node)" ng-show="node.children && node.children.length > 0" ng-class="!ctrl.isCollapsed(node) ? \'has-child\' : \'has-child-open\'"></i>' + '<i ng-click="ctrl.toggleNode(node)" class="no-child" ng-show="!node.children || node.children.length == 0"></i>' + '<span ng-bind="node.' + nodeLabel + '" ng-class="{\'selected\' : node == ctrl.treeService.selectedNode,\'faultMatched\' : node.isMatched}"></span>' + '</div>' + itemIncludeHtml + '</div>' + '<tree-view uib-collapse="!ctrl.isCollapsed(node)" tree-service="ctrl.treeService" node-children="node.children" tree-root="false" node-label="' + nodeLabel + '" item-ng-include="' + itemInclude + '" item-class="' + itemClass + '"></tree-view>' + '</li>' + '</ul>';

            if (!isRoot) {
                template = template.replace('[REPLACENODES]', '$parent.node.children');
            } else {
                template = template.replace('[REPLACENODES]', 'ctrl.treeService.nodes');
            }

            var compiledHtml = $compile(template)(scope);

            element.append(compiledHtml);
        }
    };
}]);
